package com.linked.pay.points.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.pay.points.bean.po.PointsInfoPO;
import com.linked.pay.points.service.IPointsInfoService;
import com.linked.universal.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author :dbq
 * @date : 2023/2/27 13:26
 * @description : desc
 */
@RestController
@Slf4j
@RequestMapping("pointshandle")
public class PointsHandleController {

    private final IPointsInfoService pointsInfoService;

    private final ObjectMapper mapper;

    @Autowired
    public PointsHandleController(IPointsInfoService pointsInfoService, ObjectMapper mapper) {
        this.pointsInfoService = pointsInfoService;
        this.mapper = mapper;
    }

    @PostMapping("queryPointsInfoList")
    Result queryPointsInfoList() throws Exception {

        List<PointsInfoPO> pointsList = null;
        try {
            pointsList = pointsInfoService.queryPointsInfoList();
        } catch (Exception e) {
            log.error("查询积分信息列表接口 异常", e);
            return Result.error("系统异常 请联系管理员！");
        }
        return CollectionUtils.isEmpty(pointsList) ? Result.ok(false, "未查询到数据") : Result.success(pointsList);
    }

    @PostMapping("addPoints")
    Result addPoints(@RequestBody PointsInfoPO param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("增加积分接口 入参：{}", mapper.writeValueAsString(param));
        }
        boolean ret = false;
        try {
            ret = pointsInfoService.addPoints(param);
        } catch (Exception e) {
            log.error("增加积分接口 异常", e);
            return Result.error("系统异常 请联系管理员！");
        }
        return ret ? Result.success() : Result.ok(false, "保存失败！");
    }
}
